<?php 
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}

require_once('calendar/classes/tc_calendar.php');
$_SESSION['action'] = "mybin_recedoc";
if(!isset($_REQUEST['page'])){
$list["a.Tile"]="DESC";
$list["a.Timecreate"]="DESC";
$list["a.TimeApprove"]="DESC";
$list["a.IDApproveDoc"]="DESC";
//$str=$_SESSION['sortn'];
//if($str==''){
$str="a.Timecreate";
$_SESSION['sortn']=$str;
//}
$_SESSION['sort']=$list;
}else{
	$str=$_SESSION['sortn'];
	$list=$_SESSION['sort'];
}
	if(array_key_exists("page",$_GET))
        $page  = $_GET['page'];
    else
        $page = 1;
    
    $abc = ($page-1)*10;
    
    if (isset($_REQUEST['search'])){
    	$tile=$_REQUEST['titleSearch'];
    	$fromdate=$_REQUEST['date1'];
    	$todate=$_REQUEST['date2'];
    	$search=$_REQUEST['search'];
    	$sql="SELECT SumMoney,IDApproveDoc,Tile,IDTypeDoc,DATE_FORMAT(a.TimeApprove, '%Y-%m-%d') as TimeApprove,DATE_FORMAT(a.Timecreate, '%Y-%m-%d') as Timecreate  
    	FROM approvedoc a INNER JOIN listapprove b ON a.IDGroupApprove = b.IDGroupApprove 
    	WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(".$id_employee."  USING UTF8) 
    	AND a.Tile LIKE '%$tile%' 
    	AND a.Timecreate>'".$fromdate." 00:00:00' AND a.Timecreate<'".$todate." 23:59:59' 
    	AND CONVERT(a.IDTypeDoc USING UTF8)   != 11 
    	AND CONVERT(a.IDStatus USING UTF8) = 6 
    	AND CONVERT(b.IDStatus USING UTF8) = 1";
    	$query= mysql_query($sql." ORDER BY ".$str." ".$list[$str]);
    	
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 
    }
    else {
    	$tile="";
    	$fromdate=date("Y-m-d"); 
    	$todate=date("Y-m-d"); 
    	$sql="SELECT SumMoney,IDApproveDoc,Tile,IDTypeDoc,DATE_FORMAT(a.TimeApprove, '%Y-%m-%d') as TimeApprove,DATE_FORMAT(a.Timecreate, '%Y-%m-%d') as Timecreate  FROM approvedoc a INNER JOIN listapprove b ON a.IDGroupApprove = b.IDGroupApprove WHERE CONVERT(b.IDEmployee USING UTF8) = CONVERT(".$id_employee."  USING UTF8) AND CONVERT(a.IDTypeDoc USING UTF8)   != 11 AND CONVERT(a.IDStatus USING UTF8) = 6 AND CONVERT(b.IDStatus USING UTF8) = 1";
    	$query= mysql_query($sql." ORDER BY ".$str." ".$list[$str]);
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 
    }
?>
<style>
<!--
* {margin:0;}
-->
</style>
<script type="text/javascript">
function sort(page,str,co,search,tile,from,to)
{
	

	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
											var body = document.getElementById('content');
											body.setAttribute('style','opacity:.5; position:relative;cursor: wait;  ');
											
											var d=document.createElement('div');
											d.setAttribute('style','display: block;visibility: visible; position: absolute;  z-index: 999; top: 0px; left: 0px; width: 105%; height: 105%; text-align: center;  filter: alpha(opacity=5); opacity: 0.5; ');
											
											var image=document.createElement('img');
											image.setAttribute('src','images/loading1.gif');
											image.setAttribute('style','position:absolute; left:0px; top:0px; right:0; bottom:0; margin:auto');
											
											d.appendChild(image);
											
										    body.appendChild(d);	
										    
										  if (xmlhttp.readyState==4 && xmlhttp.status==200){
										    	document.getElementById("content").innerHTML=xmlhttp.responseText;
										    	document.getElementById('content').setAttribute('style','opacity:1; position:relative;');
										  }
									  }
	xmlhttp.open("GET","ajax/list/sort_mybin_re.php?page="+page+"&str="+str+"&co="+co+"&search="+encodeURI(search)+"&titleSearch="+tile+"&date1="+from+"&date2="+to,true);
	xmlhttp.send();
}

</script>
<div class="title"></div>
                    <div class="content_code">
						<div class="approval-right">
							<form action="mybin.php" method="get">
							<table width="100%" >
								<tr>
									<td>
										<div class="title_content"><?=$l_re_bin ?> &rarr; <?=$l_re_document ?></div>
						
						<input type="hidden" name="content" value="list-mybin-recedoc-content" />
						<table  border="1" bordercolor="#C5D1E8" cellpadding="4"  style="border-collapse:collapse;width: 75%">
							<tr height="40">
							<td align="center" style="vertical-align:middle;width: 120px"><?=$l_created_date?></td>
							<td align="center" style="height:24px;">
							<?php
							  $myCalendar1 = new tc_calendar("date1", true);
							  $myCalendar1->setIcon("calendar/images/iconCalendar.gif");
							  $d=split('-',$fromdate,3);
							 
							  $myCalendar1->setDate($d[2],$d[1],$d[0]);
							  $myCalendar1->setPath("calendar/");
							  $myCalendar1->setYearInterval(2010, 2015);
							  $myCalendar1->dateAllow('2010-01-01', '2015-12-31');
							  //$myCalendar1->setHeight(350);
							  //$myCalendar1->autoSubmit(true, "form1");
							  //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
							  $myCalendar1->setOnChange("myChanged('test')");
							  //$myCalendar->rtl = true;
							  $myCalendar1->writeScript();
		
							  //echo $myCalendar1->getDate();
							  
							  ?>
							</td >
							
							  <td align="center" style="height:24px;">
							  <?php
							  $myCalendar2 = new tc_calendar("date2", true);
							  $myCalendar2->setIcon("calendar/images/iconCalendar.gif");
							  $d=split('-',$todate,3);
							 
							  $myCalendar2->setDate($d[2],$d[1],$d[0]);
							  $myCalendar2->setPath("calendar/");
							  $myCalendar2->setYearInterval(2010, 2015);
							  $myCalendar2->dateAllow('2010-01-01', '2015-12-31');
							  //$myCalendar1->setHeight(350);
							  //$myCalendar1->autoSubmit(true, "form1");
							  //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
							  $myCalendar2->setOnChange("myChanged('test')");
							  //$myCalendar->rtl = true;
							  $myCalendar2->writeScript();
							  
							  ?>	
                   							  	<!--  >input type="hidden" name="totime" value="<?php echo $myCalendar2->getDate();?>" />
                   							  	<input type="hidden" name="fromtime" value="<?php echo $myCalendar1->getDate();?>" /-->
											
							</td>												
							</tr>
							<tr height="40">
							<td align="center" style="vertical-align:middle"><?=$l_title?> </td>
							<td colspan="2" style="padding-left: 3px;padding-right: 3px;">
								<input type="text" name="titleSearch" value="<?=$tile?>" style="width:99%;height:20px;" /></td>
							</tr>
						</table>	
							
												
							
					</td>
			</tr>
			<tr>
				<td colspan="2" align="left">
					<table width="70%">
					<tr><td align="center">
						<input style="margin:12px 5px 0 0; font-size: 12px;" type="submit" value="<?=$l_search?>" class="btn_choose" name="search" />
						<input style="margin:12px 5px 0 0; font-size: 12px;" type="submit" name="all" value="<?=$l_all?>" class="btn_choose" />
					</td></tr></table>
				</td>
			
			</tr>
			<?php 
											    	$tile=$_REQUEST['titleSearch'];
											    	$fromdate=$_REQUEST['date1'];
											    	$todate=$_REQUEST['date2'];
											    	$search=$_REQUEST['search'];
													//page,str,co,search,tile,from,to,IdSend)
								?>
			
			<tr>
				<td>										
					<div style=" margin-top:30px" >
					<div id='content'>
						<table width="100%" class="table_th" style="height:22px;" cellspacing="0">
							
							<tr class="trtit" style="background-color:#EEE;">
								<td width="3%"><?=$l_no?></td>
								<!--td width="10%"><a onclick="sort(<?=$page?>,'a.IDApproveDoc','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')">ID</a></td-->
					            <td width="15%"><?=$l_typedoc?></td>
					            <td width="30%"><a onclick="sort(<?=$page?>,'a.Tile','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_title?></a></td>
					 
					            <td width="10%"><a onclick="sort(<?=$page?>,'a.Timecreate','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_created_date?></a></td>
					            <td width="10%"><a onclick="sort(<?=$page?>,'a.TimeApprove','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_approve_date?></a></td>
							
					          
					            
					        </tr>
							
							
							
							<?php
							
						    $i = 0; 
						    while ($row=mysql_fetch_array($query)){
						    	if ( $i >= $page_start  ) 
								{	
							    	if($row[IDTypeDoc]==7)$document = $l_vacation_form;
								    else if($row[IDTypeDoc]==12)$document = $l_assignment_report;
								    else if($row[IDTypeDoc]==13)$document = $l_purchase_order;
								    else if($row[IDTypeDoc]==14)$document = $l_daily_report;
								    else if($row[IDTypeDoc]==15)$document = $l_business_trip_report;
								    else if($row[IDTypeDoc]==16)$document = $l_weekly_report;
								    else if($row[IDTypeDoc]==17)$document = $l_susqence_payment_request;
								    else if($row['IDTypeDoc']==18)$document = $l_payment_request;
								    else if($row[IDTypeDoc]==19)$document = $l_purchase_order1;
							    
							?>
								<tr bgcolor="#ffffff" class="highlighter">
									<td width="3%" align="center"><?=($i+1)?></td>
									<!--td ><a href="mybin.php?id=<?php echo $row[IDApproveDoc]?>&type=<?php echo $row[IDTypeDoc]?>"><?php echo $row[IDApproveDoc];?></a></td-->
									<td ><div  align="left" ><?php echo $document;?></div></td>
									<td ><div  align="left" ><a href='mybin&<?=$row[IDApproveDoc]?>&<?=$row[IDTypeDoc]?>&<?=$page?>'><?=$row[Tile]?></a>
										<p style="float:right">
															<?php 
																if($row[IDTypeDoc]==13 || $row[IDTypeDoc]==15 || $row[IDTypeDoc]==17 || $row[IDTypeDoc]==19){
																	if($row[SumMoney]>0){
																		echo " [ ".number_format($row[SumMoney])." ]";
																	}else{
																		if($row[IDTypeDoc]==15 || $row[IDTypeDoc]==13 || $row[IDTypeDoc]==19){
																			$s1="select sum(buying_document.total) as tongtien from approvedoc,buying_document where approvedoc.IDApproveDoc=buying_document.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}else if($row[IDTypeDoc]==17){
																			$s1="select sum(ap_advance.money) as tongtien from approvedoc,ap_advance where approvedoc.IDApproveDoc=ap_advance.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}
																	}
																}
															?>
															</p>
									</div></td>
									<td ><?=$row[Timecreate]?></td>
									<td ><?=$row[TimeApprove]?></td>
									
								</tr>
							<?php 	}
							
								$i++; 
								
								if ($i >= $page_end) 
								{ 
								break; 
								} 
							}
							
								
							?>
						
						<tr>
								<td colspan="5" class="bt_td_main">
								
								<span style="background:#fff; padding:1px; display: inline-block;">
														
														<?php if ( $number_of_page > 1 ) 
														{ 
															if ($page==1)
																$p=2;
															else if ($page==$number_of_page)
																$p=$number_of_page-1;
															else $p=$page;
															if($page!=1){?>
																<a onclick="sort(1,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo "<<<";?></span> </a>
																<a onclick="sort(<?=($page-1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo "<<";?></span> </a>
															<?php  }
															else{
																echo "<span class='button_next'><<<</span>"; 
																echo "<span class='button_next'><<</span>";
															}
															for ( $i = $p-1; $i <= $p+1; $i++ ) {	
																	if ($i<=$number_of_page && $i>0)
																	
																		if ($i==$page){
																			echo "<span class='button_next'><b>".$i."</b></span>"; 
																		}else{
																		?>
																			<a onclick="sort(<?=$i?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class="button_next" style="color: #508e21;">
																			<?php 
																			echo $i;
																			?>
																			</span> </a>
																		<?php 
																		}
																	?>
																
															<?php 
																	}
																
																if($page!=$number_of_page){ ?>
																	<a onclick="sort(<?=($page+1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><span class='button_next'><?php echo ">>";?></span> </a>
																	<a onclick="sort(<?=($number_of_page)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo ">>>";?></span> </a>
																<?php }
																else{
																	echo "<span class='button_next'>>></span> ";
																	echo "<span class='button_next'>>>></span> ";
																}
															}
														
															
															?>
																												
													</span>
								</td>
							</tr>
						</table>
						</div>
					</div>
				</td>
			</tr>
		</table>
		</form>	
	</div>
</div>